package com.xb.cloud.shopping.client;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.xb.cloud.gateway.client.Adapter;
import com.xb.cloud.gateway.client.AdapterContext;
import com.xb.cloud.gateway.client.ReturnCodes;
import com.xb.cloud.gateway.client.ReturnUtils;
import com.xb.cloud.gateway.client.RtnInfo;
import com.xb.cloud.shopping.service.PaymentApiService;
import com.xb.cloud.shopping.service.client.vo.PaymentRequestVo;

@Component
public class ShoppingPaymentAdapter extends Adapter {

	@Resource
	PaymentApiService paymentApiService;

	@Override
	public RtnInfo execute(String urlPath, Boolean isLogined, Map<String, String> bizInfo, AdapterContext context) {
		PaymentRequestVo requestVo = new PaymentRequestVo();

		if (null == bizInfo.get("shopId")) {
			return ReturnUtils.error(ReturnCodes.AUTO_USER_TOKEN_INVALID);
		}
		requestVo.setShopId(Long.valueOf(bizInfo.get("shopId")));

		if (urlPath.equalsIgnoreCase("/h5/shopping/payment/receiptCode")) {
			RtnInfo rtnInfo = paymentApiService.getReceiptCode(requestVo);
			return rtnInfo;
		}

		Long companyId = context.getCompanyId();
		Long currentUserId = context.getCurrentUserId();
		requestVo.setCompanyId(companyId);
		requestVo.setCurrentUserId(currentUserId);
		
		if (urlPath.equalsIgnoreCase("/h5/shopping/payment/payOrder")) {
			requestVo.setOrderId(Long.valueOf(bizInfo.get("orderId")));
			requestVo.setPmtTag(bizInfo.get("pmtTag"));
			requestVo.setPmtName(bizInfo.get("pmtName"));
			RtnInfo rtnInfo = paymentApiService.payOrder(requestVo);
			return rtnInfo;
		}

		if (urlPath.equalsIgnoreCase("/h5/shopping/payment/getPayList")) {
			RtnInfo rtnInfo = paymentApiService.getPayList(requestVo);
			return rtnInfo;
		}

		return null;
	}

	@Override
	public String getUrlMappingPath() {
		return "/h5/shopping/payment/**";
	}

	@Override
	public boolean isCheckUserToken() {
		return true;
	}

}
